package com.haohan.jingyuan.sys.controller;

import cn.hutool.json.JSONUtil;
import com.haohan.jingyuan.gateway.aspect.NeedLogin;
import com.haohan.jingyuan.gateway.dto.GwRequest;
import com.haohan.jingyuan.gateway.dto.GwResponse;
import com.haohan.jingyuan.sys.dto.req.FileUrlRequest;
import com.haohan.jingyuan.sys.dto.resp.FileUrlResp;
import com.haohan.jingyuan.util.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * 文件上传控制器
 * 提供文件上传到OSS的功能
 *
 * @author haohan
 * @since 2025-01-27
 */
@Slf4j
@RestController
public class FileUploadController {

    @Autowired
    private OssUtil ossUtil;

    /**
     * 上传文件到OSS
     *
     * @param file 上传的文件
     * @return 上传结果，包含文件访问URL
     */
    @NeedLogin
    @PostMapping("/sys/file/upload")
    public GwResponse<String> uploadFile(@RequestParam("file") MultipartFile file) {
        log.info("文件上传请求，文件名: {}, 文件大小: {} bytes", file.getOriginalFilename(), file.getSize());

        try {
            // 调用OSS工具类上传文件
            String objectName = ossUtil.uploadFile(file);

            log.info("文件上传成功，objectName: {}", objectName);
            return GwResponse.success("文件上传成功", objectName);
        } catch (Exception e) {
            log.error("文件上传失败", e);
            return GwResponse.error("文件上传失败: " + e.getMessage());
        }
    }

    /**
     * 批量上传文件到OSS
     *
     * @param files 上传的文件数组
     * @return 上传结果，包含文件访问URL数组
     */
    @NeedLogin
    @PostMapping("/sys/file/batchUpload")
    public GwResponse<String[]> uploadFiles(@RequestParam("files") MultipartFile[] files) {
        log.info("批量文件上传请求，文件数量: {}", files.length);
        try {
            String[] objectNames = new String[files.length];

            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];
                if (file != null && !file.isEmpty()) {
                    objectNames[i] = ossUtil.uploadFile(file);
                    log.info("文件 {} 上传成功，访问URL: {}", file.getOriginalFilename(), objectNames[i]);
                }
            }

            log.info("批量文件上传完成，成功上传 {} 个文件，objectNames={}", files.length, objectNames);
            return GwResponse.success(objectNames);

        } catch (Exception e) {
            log.error("批量文件上传失败", e);
            return GwResponse.error("批量文件上传失败: " + e.getMessage());
        }
    }

    /**
     * 获取文件临时访问URL
     *
     * @param request 获取文件URL请求
     * @return 包含临时URL的响应
     */
    @PostMapping("/sys/file/url")
    public GwResponse<FileUrlResp> getFileUrl(@Valid @RequestBody GwRequest<FileUrlRequest> request) {
        log.info("FileUploadController.getFileUrl BEGIN ==> {}", JSONUtil.toJsonStr(request));
        String objectName = request.getRequestData() == null ? null : request.getRequestData().getObjectName();
        String url = ossUtil.getFileUrl(objectName);
        FileUrlResp resp = new FileUrlResp();
        resp.setUrl(url);
        GwResponse<FileUrlResp> ok = GwResponse.success("获取文件URL成功", resp);
        log.info("FileUploadController.getFileUrl END ==> {}", JSONUtil.toJsonStr(ok));
        return ok;
    }
}
